package org.szgb.console.sys.role.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.szgb.console.sys.role.entity.SysRole;
import org.szgb.console.sys.role.mapper.SysRoleMapper;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.UUIDUtil;

import java.util.List;

@Slf4j
@Service
public class SysRoleService {
    private final SysRoleMapper sysRoleMapper;

    @Autowired
    public SysRoleService(SysRoleMapper sysRoleMapper) {
        this.sysRoleMapper = sysRoleMapper;
    }

    @Transactional(readOnly = true)
    public SysRole get(String id) {
        return sysRoleMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<SysRole> findList(SysRole sysRole) {
        if (sysRole != null && sysRole.getPageDto() != null) {
            Page<SysRole> page = new Page<>(sysRole.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            sysRole.setPage(page);
        }
        return sysRoleMapper.findList(sysRole);
    }

    @Transactional(readOnly = true)
    public Integer getCount(SysRole sysRole) {
        return sysRoleMapper.getCount(sysRole);
    }

    @Transactional()
    public void save(SysRole sysRole) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (sysRole.getId() == null || sysRole.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            sysRole.setCreatedBy(sysRole.getUpdatedBy());
            // 添加UUID
            sysRole.setId(UUIDUtil.getUUID());
            // 执行插入操作
            sysRoleMapper.insert(sysRole);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            sysRoleMapper.update(sysRole);
        }
    }

    @Transactional
    public void delete(String ids) {
        sysRoleMapper.delete(ids.split(","));
    }
}
